<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">welcome</div>
      <!-- 登录表单 -->
      <el-form
        status-icon
        :model="loginForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            placeholder="用户名"
            v-model="loginForm.username"
            @keyup.enter.native="login"
          />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            show-password
            placeholder="密码"
            v-model="loginForm.password"
            @keyup.enter.native="login"
          />
        </el-form-item>
      </el-form>
      <!-- 登录按钮 -->
      <el-button type="primary" @click="login">登录</el-button>
    </div>
  </div>
</template>

<script>
import { generaMenu } from "../../assets/js/menu";
export default {
  name: "Login",
  data: function (){
    return{
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{required: true, message: "用户名不能为空", trigger: "blur"}],
        password: [{required: true, message: "密码不能为空", trigger: "blur"}]
      }
    };
  },
  methods: {
    login() {
      this.$refs.ruleForm.validate(valid => {
        if (valid){
          const that = this;
          const captcha = new TencentCaptcha(
              that.config.TENCENT_CAPTCHA,
              function (res) {
                if (0 === res.ret) {
                  //发送登录请求
                  let param = new URLSearchParams();
                  param.append("username", that.loginForm.username);
                  param.append("password", that.loginForm.password);
                  that.axios.post("/api/login", param).then(({data}) => {
                    if (data.success) {
                      //登陆后保存用户信息
                      console.log(data);
                      that.$store.commit("login", data.data);
                      //加载用户菜单
                      generaMenu();
                      that.$message.success(data.message)
                      that.$router.push({path: "/"});
                    } else {
                      that.$message.error(data.message);
                    }
                  })
                }
              }
          );
          //显示验证码
          captcha.show();
        } else {
          return false;
        }
      });
    }
  },
};
</script>

<style>
.login-container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: url("../../assets/img/bg.png") center center /
    cover no-repeat;
  background-size: 100% auto;
}
.login-card {
  position: fixed;
  top:50%;
  left:50%;
  transform: translate(-50%,-50%);

  width: 350px;
  opacity: 0.9;
}
.login-title {
  text-align: center;
  color: #303133;
  font-weight: bold;
  font-size: 2rem;
}
.login-form {
  margin-top: 1.2rem;
}
.login-card button {
  margin-top: 1rem;
  width: 100%;
}
</style>